App = {
	web3Provider: null,
	contracts: {},
	addr: "0x0c74ff77df9afe7a2d1fa08361af5c34267df881",
	init: function() {
		return App.initWeb3();
	},
	initWeb3: function() {
		if(typeof web3 != 'undefined') {
			console.log("metamask");
			App.web3Provider = web3.currentProvider;
		} else {
			console.log("local");
			return false;
		}
		web3 = new Web3(App.web3Provider);
		return App.initContract();
	},

	initContract: function() {
		$.getJSON(BCConfig.url + 'res/RPS.json', function(data) {
			var AdoptionArtifact = data;
			App.contracts.Adoption = TruffleContract(AdoptionArtifact);
			App.contracts.Adoption.setProvider(App.web3Provider);
			BC.bcInstance = App.contracts.Adoption.at(App.addr);
			console.log("初始化成功:"+data);
			for(var i = 0; i < BC.onFinish.length; i++) {
				BC.onFinish[i]();
			}
			return "";
		});
	},
	getAcount: function() {
		var p = new Promise(function(resolve, reject) {
			if(typeof web3 == 'undefined') {
				reject({
					code: 1,
					msg: "未检测到MetaMask账号登录，您必须先登录后才能开玩。"
				});
			} else {
				var acc = web3.eth.getAccounts(function(err, accs) {
					console.log(err);
					console.log(accs);
					if(accs.length >= 1) {
						App.accounts = accs;
						resolve(accs);
					} else {
						reject({
							code: 1,
							msg: "未检测到MetaMask账号登录，您必须先登录后才能开玩。"
						});
					}

				});
			}
		});
		return p;
	},
	canUse: function() {
		var p = new Promise(function(resolve, reject) {
			App.getAcount().then(function(data) {
				resolve(data);
			}).catch(function(err) {
				reject(err);
			});
		});
		return p;
	}
};

BC = {
	onFinish: [],
	do: function(type, param) {
		var prom = new Promise(function(resolve, reject) {
			var p;
			if(typeof BC.bcInstance == "undefined" || typeof BC.bcInstance.create == "undefined") {
				setTimeout(function() {
					reject({
						code: 2,
						msg: "请等待初始化结束",
						data: ""
					});
				}, 1000);

				return;
			}
			if(type == "create") {
				p = BC.bcInstance.create({
					value: param.value,
					from: App.accounts[0]
				});
			} else if(type == 'challenge') {
				p = BC.bcInstance.challenge(param.hashId, {
					value: param.value
				});
			} else if(type == 'generateBlindedBid') {
				p = BC.bcInstance.generateBlindedBid(param.choice, param.secret);
			} else if(type == 'bid') {
				p = BC.bcInstance.bid(param.hashId, param.blindedBid, {});
			} else if(type == 'reveal') {
				p = BC.bcInstance.reveal(param.hashId, param.choice, param.secret, {})
			} else if(type == 'checkAndSettle') {
				p = BC.bcInstance.settle(param.hashId, {});
			} else if(type == 'destroy') {
				p = BC.bcInstance.destroy(param.gameId);
			} else if(type == 'query') {
				p = BC.bcInstance.query.call(param.hashId);
			} else if(type == 'canAction') {
				resolve([{
					c: [0, 0]
				}]);
			}
			p.then(function(result) {
				resolve(result);
			}).catch(function(err) {
				reject({
					code: 2,
					msg: err.message ? "您拒绝了交易" : "未知错误",
					data: err
				});
			});
		});

		return prom;

	},
	//"0x0a25df08a04e2fedad3313dcaf00dd84a2657ba9"
	getBalance: function() {
		var p = new Promise(function(resolve, reject) {
			var addr = App.accounts[0];
			var contractBalance = web3.eth.getBalance(addr, function(a, b) {
				resolve(b);
			});
		});
		return p;

	}
}